package com.joolun.system.service.impl;

import java.util.List;
import com.joolun.common.utils.DateUtils;
import com.joolun.system.domain.EduCourse;
import com.joolun.system.domain.PayLink;
import com.joolun.system.domain.PayParam;
import com.joolun.system.mapper.EduCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import com.joolun.system.mapper.PayOrderMapper;
import com.joolun.system.domain.PayOrder;
import com.joolun.system.service.IPayOrderService;

/**
 * 课程订单Service业务层处理
 * 
 * @author joolun
 * @date 2025-07-07
 */
@Service
public class PayOrderServiceImpl extends  ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService
{
    @Autowired
    private PayOrderMapper payOrderMapper;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    /**
     * 查询课程订单
     * 
     * @param id 课程订单主键
     * @return 课程订单
     */
    @Override
    public PayOrder selectPayOrderById(Long id)
    {
        return payOrderMapper.selectPayOrderById(id);
    }

    /**
     * 查询课程订单列表
     * 
     * @param payOrder 课程订单
     * @return 课程订单
     */
    @Override
    public List<PayOrder> selectPayOrderList(PayOrder payOrder)
    {
        return payOrderMapper.selectPayOrderList(payOrder);
    }

    /**
     * 新增课程订单
     *
     * @param payOrder 订单基础参数
     * @param payLink 支付链接
     * @return 结果 返回创建的订单对象
     */
    @Override
    public PayOrder insertPayOrder(PayOrder payOrder, PayLink payLink)
    {
        // 创建订单
        PayOrder newPayOrder = new PayOrder();
        // 用户唯一标识
        newPayOrder.setUnionId(payOrder.getUnionId());
        // 订单创建时间
        newPayOrder.setCreateTime(DateUtils.getNowDate());
        // 订单号
        newPayOrder.setOrderNo(payOrder.getOrderNo());
        // 获取课程信息
        EduCourse eduCourse = eduCourseMapper.selectEduCourseByCourseId(Long.parseLong(payLink.getCourseId()));
        if("1".equals(payLink.getTitleType())){
            // 关联课程标题
            newPayOrder.setName(eduCourse.getCourseName());
        }else if("2".equals(payLink.getTitleType())){
            // 红包支付名称
            newPayOrder.setName(payLink.getName());
        }else if("3".equals(payLink.getTitleType())){
            // 自定义
            newPayOrder.setName(payLink.getTitleName());
        }
        // 支付金额单位：分
        newPayOrder.setPaymentPrice(eduCourse.getPaymentAmount());
        // 课程详情
        newPayOrder.setOrderDetail(eduCourse.getCourseDescribe());
        // 主体id
        newPayOrder.setCorpId(payLink.getCorpId());
        int num = payOrderMapper.insertPayOrder(newPayOrder);
        if(num > 0){
            // 创建成功
            return newPayOrder;
        }else {
            // 创建失败
            return null;
        }
    }

    /**
     * 修改课程订单
     * 
     * @param payOrder 课程订单
     * @return 结果
     */
    @Override
    public int updatePayOrder(PayOrder payOrder)
    {
        payOrder.setUpdateTime(DateUtils.getNowDate());
        return payOrderMapper.updatePayOrder(payOrder);
    }

    /**
     * 批量删除课程订单
     * 
     * @param ids 需要删除的课程订单主键
     * @return 结果
     */
    @Override
    public int deletePayOrderByIds(Long[] ids)
    {
        return payOrderMapper.deletePayOrderByIds(ids);
    }

    /**
     * 删除课程订单信息
     * 
     * @param id 课程订单主键
     * @return 结果
     */
    @Override
    public int deletePayOrderById(Long id)
    {
        return payOrderMapper.deletePayOrderById(id);
    }
}
